﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using VehicleManagement.ControllersServices;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper;
namespace VehicleManagement.Controllers
{
    [CustomerAuthorize(Roles = "管理员,负责人,申请人,财务人员,司机")]
    public class MemoryBookController : Controller
    {
        //
        // GET: /MemoryBook/
        MemoryBookServices memoryServices = new MemoryBookServices();
        public ActionResult Index()
        {
            return View(memoryServices.GetAllMemoryRecord());
        }

        /// <summary>
        /// 返回代办事记的视图
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateMemoryRecord()
        {
            return View();
        }

        /// <summary>
        /// 客户端提交表单时，创建购件信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateMemoryRecord(MemoryBook memoryrecord)
        {
            if (ModelState.IsValid)
            {

                object obj = memoryServices.CreateMemoryRecord(memoryrecord);
                return RedirectToAction("Index");

            }
            else
                return View(memoryrecord);
        }

        /// <summary>
        /// 根据司机ID，查看司机详细信息，并返回视图
        /// </summary>
        /// <param name="id">司机的ID</param>
        /// <returns>司机信息详细视图</returns>
        public ActionResult DetailsMemoryRecord(int id)
        {
            return View(memoryServices.GetMemoryRecordByID(id));
        }


        /// <summary>
        /// 备忘事记修改
        /// </summary>
        /// <param name="id">代办ID</param>
        /// <param name="driveriofo">代办事记类</param>
        /// <returns>修改司机的视图</returns>
        public ActionResult EditMemoryRecord(int id)
        {
            MemoryBook memoryrecord = memoryServices.GetMemoryRecordByID(id);

            return View(memoryrecord);
        }
        [HttpPost]
        public ActionResult EditMemoryRecord(MemoryBook memoryrecord)
        {
            if (ModelState.IsValid)
            {
                int rows = memoryServices.EditMemoryRecord(memoryrecord);
                return RedirectToAction("Index");
            }
            return View(memoryrecord);

        }

        /// <summary>
        /// 根据代办事记的ID，删除代办事记
        /// </summary>
        /// <param name="id">代办ID</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            MemoryBook memoryrecord = memoryServices.GetMemoryRecordByID(id);
            return View(memoryrecord);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (Request.IsAjaxRequest())
            {
                int i = memoryServices.DeleteMemoryRecord(id);//根据代办事记ID，删除
                if (i == 1)//删除成功
                {
                    return Json(new { Result = "true", Message = "删除成功" });
                }
                else//删除不成功
                {
                    return Json(new { Result = "false", Message = "删除失败" });
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }

        }



    }
}
